<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="queryParams.keyword"
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="changeValue"
        />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          :data="treeList"
          :props="defaultProps"
          node-key="id"
          check-strictly
          default-expand-all
          :expand-on-click-node="false"
          highlight-current
          @current-change="handleCurrentChange"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="showExcelDialog=true">excel导入</el-button>
          <!-- <el-button v-if="$store.state.user.userInfo.roles.points.includes('excel')" size="mini" @click="exportData">excel导出</el-button> -->
          <el-button v-allow="'excel'" size="mini" @click="exportData">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="employeeList">
          <el-table-column prop="staffPhoto" align="center" label="头像">
            <template v-slot="{row}">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" :size="30" />
              <span v-else class="username">{{ row.username.charAt(0) }}
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式" />
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template v-slot="{row}">
              <el-button size="mini" type="text" @click="$router.push('/employee/detail/'+row.id)">查看</el-button>
              <el-button size="mini" type="text" @click="hAssionRole(row.id)">角色</el-button>
              <el-popconfirm title="确认删除该行数据吗？" @onConfirm="confirmDel(row.id)">
                <el-button slot="reference" style="margin-left:10px" size="mini" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination
            layout="total,prev, pager, next"
            :total="total"
            :current-page="queryParams.page"
            :page-size="queryParams.pagesize"
            @current-change="hangdleCurrentPage"
          />
        </el-row>
        <!-- 分页 -->
      </div>
      <import-employee :show-excel-dialog.sync="showExcelDialog" @uploadSuccess="loadEmployees" />
    </div>
    <AssionRole
      ref="AssionRole"
      :show-assign-dialog.sync="showAssignDialog"
      :current-id="currentId"
    />
  </div>
</template>

<script>
import AssionRole from './components/assignRole.vue'
import { transListToTreeData } from '@/utils/index'
import { getDepartment } from '@/api/department'
import { getEmployeeList, exportEmployees, delEmployees } from '@/api/employee'
import FileSaver from 'file-saver'
import ImportEmployee from './components/import-employee'
export default {
  name: 'Employee',
  components: {
    ImportEmployee,
    AssionRole
  },
  data() {
    return {
      treeList: [],
      employeeList: [],
      total: 0,
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      id: '',
      queryParams: {
        page: 1,
        pagesize: 10,
        keyword: null,
        departmentId: null
      },
      showExcelDialog: false,
      showAssignDialog: false,
      currentId: -1
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      const res = await getDepartment()
      this.treeList = transListToTreeData(res.data, 0)
      const id = res.data[0].id
      this.$nextTick(() => this.$refs.tree.setCheckedKeys([id]))
    },
    async loadEmployees() {
      console.log(this.queryParams)
      const res = await getEmployeeList(this.queryParams)
      console.log(res)
      const { rows, total } = res.data
      this.employeeList = rows
      this.total = total
    },
    handleCurrentChange(data, node) {
      this.queryParams.departmentId = data.id
      console.log(this.queryParams.departmentId)
      this.loadEmployees()
      console.log(data)
    },
    async exportData() {
      const res = await exportEmployees()
      console.log(res)
      FileSaver.saveAs(res, '员工信息表')
    },
    hangdleCurrentPage(newPage) {
      this.queryParams.page = newPage
      this.loadEmployees()
    },
    changeValue() {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.queryParams.page = 1
        this.loadEmployees()
      }, 300)
    },
    async confirmDel(id) {
      const res = await delEmployees(id)
      console.log(res.message)
      if (this.treeList.length === 1 && this.treeList > 1) { this.queryParams.page-- }
      this.$message.success(res.message)
      this.loadEmployees()
    },
    async  hAssionRole(id) {
      this.showAssignDialog = true
      this.currentId = id
      console.log(this.currentId)
      this.$nextTick(() => {
        this.$refs.AssionRole.getRoleIds()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}
</style>

